﻿package org.flixel
{
    import flash.display.*;
    import flash.text.*;

    public class FlxText extends FlxSprite
    {
        protected var _textField:TextField;
        protected var _regen:Boolean;
        protected var _shadow:uint;

        public function FlxText(param1:Number, param2:Number, param3:uint, param4:String = null, param5:Boolean = true)
        {
            super(param1, param2);
            makeGraphic(param3, 1, 0);
            if (param4 == null)
            {
                param4 = "";
            }
            this._textField = new TextField();
            this._textField.width = param3;
            this._textField.embedFonts = param5;
            this._textField.selectable = false;
            this._textField.sharpness = 100;
            this._textField.multiline = true;
            this._textField.wordWrap = true;
            this._textField.text = param4;
            var _loc_6:* = new TextFormat("system", 8, 16777215);
            this._textField.defaultTextFormat = _loc_6;
            this._textField.setTextFormat(_loc_6);
            if (param4.length <= 0)
            {
                this._textField.height = 1;
            }
            else
            {
                this._textField.height = 10;
            }
            this._regen = true;
            this._shadow = 0;
            allowCollisions = NONE;
            this.calcFrame();
            return;
        }// end function

        override public function destroy() : void
        {
            this._textField = null;
            super.destroy();
            return;
        }// end function

        public function setFormat(param1:String = null, param2:Number = 8, param3:uint = 16777215, param4:String = null, param5:uint = 0) : FlxText
        {
            if (param1 == null)
            {
                param1 = "";
            }
            var _loc_6:* = this.dtfCopy();
            this.dtfCopy().font = param1;
            _loc_6.size = param2;
            _loc_6.color = param3;
            _loc_6.align = param4;
            this._textField.defaultTextFormat = _loc_6;
            this._textField.setTextFormat(_loc_6);
            this._shadow = param5;
            this._regen = true;
            this.calcFrame();
            return this;
        }// end function

        public function get text() : String
        {
            return this._textField.text;
        }// end function

        public function set text(param1:String) : void
        {
            var _loc_2:* = this._textField.text;
            this._textField.text = param1;
            if (this._textField.text != _loc_2)
            {
                this._regen = true;
                this.calcFrame();
            }
            return;
        }// end function

        public function get size() : Number
        {
            return this._textField.defaultTextFormat.size as Number;
        }// end function

        public function set size(param1:Number) : void
        {
            var _loc_2:* = this.dtfCopy();
            _loc_2.size = param1;
            this._textField.defaultTextFormat = _loc_2;
            this._textField.setTextFormat(_loc_2);
            this._regen = true;
            this.calcFrame();
            return;
        }// end function

        override public function get color() : uint
        {
            return this._textField.defaultTextFormat.color as uint;
        }// end function

        override public function set color(param1:uint) : void
        {
            var _loc_2:* = this.dtfCopy();
            _loc_2.color = param1;
            this._textField.defaultTextFormat = _loc_2;
            this._textField.setTextFormat(_loc_2);
            this._regen = true;
            this.calcFrame();
            return;
        }// end function

        public function get font() : String
        {
            return this._textField.defaultTextFormat.font;
        }// end function

        public function set font(param1:String) : void
        {
            var _loc_2:* = this.dtfCopy();
            _loc_2.font = param1;
            this._textField.defaultTextFormat = _loc_2;
            this._textField.setTextFormat(_loc_2);
            this._regen = true;
            this.calcFrame();
            return;
        }// end function

        public function get alignment() : String
        {
            return this._textField.defaultTextFormat.align;
        }// end function

        public function set alignment(param1:String) : void
        {
            var _loc_2:* = this.dtfCopy();
            _loc_2.align = param1;
            this._textField.defaultTextFormat = _loc_2;
            this._textField.setTextFormat(_loc_2);
            this.calcFrame();
            return;
        }// end function

        public function get shadow() : uint
        {
            return this._shadow;
        }// end function

        public function set shadow(param1:uint) : void
        {
            this._shadow = param1;
            this.calcFrame();
            return;
        }// end function

        override protected function calcFrame() : void
        {
            var _loc_1:uint = 0;
            var _loc_2:uint = 0;
            var _loc_3:TextFormat = null;
            var _loc_4:TextFormat = null;
            if (this._regen)
            {
                _loc_1 = 0;
                _loc_2 = this._textField.numLines;
                height = 0;
                while (_loc_1 < _loc_2)
                {
                    
                    height = height + this._textField.getLineMetrics(_loc_1++).height;
                }
                height = height + 4;
                _pixels = new BitmapData(width, height, true, 0);
                frameHeight = height;
                this._textField.height = height * 1.2;
                _flashRect.x = 0;
                _flashRect.y = 0;
                _flashRect.width = width;
                _flashRect.height = height;
                this._regen = false;
            }
            else
            {
                _pixels.fillRect(_flashRect, 0);
            }
            if (this._textField != null && this._textField.text != null && this._textField.text.length > 0)
            {
                _loc_3 = this._textField.defaultTextFormat;
                _loc_4 = _loc_3;
                _matrix.identity();
                if (_loc_3.align == "center" && this._textField.numLines == 1)
                {
                    _loc_4 = new TextFormat(_loc_3.font, _loc_3.size, _loc_3.color, null, null, null, null, null, "left");
                    this._textField.setTextFormat(_loc_4);
                    _matrix.translate(Math.floor((width - this._textField.getLineMetrics(0).width) / 2), 0);
                }
                if (this._shadow > 0)
                {
                    this._textField.setTextFormat(new TextFormat(_loc_4.font, _loc_4.size, this._shadow, null, null, null, null, null, _loc_4.align));
                    _matrix.translate(1, 1);
                    _pixels.draw(this._textField, _matrix, _colorTransform);
                    _matrix.translate(-1, -1);
                    this._textField.setTextFormat(new TextFormat(_loc_4.font, _loc_4.size, _loc_4.color, null, null, null, null, null, _loc_4.align));
                }
                _pixels.draw(this._textField, _matrix, _colorTransform);
                this._textField.setTextFormat(new TextFormat(_loc_3.font, _loc_3.size, _loc_3.color, null, null, null, null, null, _loc_3.align));
            }
            if (framePixels == null || framePixels.width != _pixels.width || framePixels.height != _pixels.height)
            {
                framePixels = new BitmapData(_pixels.width, _pixels.height, true, 0);
            }
            framePixels.copyPixels(_pixels, _flashRect, _flashPointZero);
            return;
        }// end function

        protected function dtfCopy() : TextFormat
        {
            var _loc_1:* = this._textField.defaultTextFormat;
            return new TextFormat(_loc_1.font, _loc_1.size, _loc_1.color, _loc_1.bold, _loc_1.italic, _loc_1.underline, _loc_1.url, _loc_1.target, _loc_1.align);
        }// end function

    }
}
